package com.yiyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yiyi.web.controller.utils.IpAddressUtil;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yiyi.common.annotation.Log;
import com.yiyi.common.core.controller.BaseController;
import com.yiyi.common.core.domain.AjaxResult;
import com.yiyi.common.enums.BusinessType;
import com.yiyi.system.domain.CsIpWxCode;
import com.yiyi.system.service.ICsIpWxCodeService;
import com.yiyi.common.utils.poi.ExcelUtil;
import com.yiyi.common.core.page.TableDataInfo;

/**
 * ip地址与企业微信匹配Controller
 *
 * @author yiyi
 * @date 2024-10-18
 */
@RestController
@RequestMapping("/system/ipWxCode")
public class CsIpWxCodeController extends BaseController
{
    @Autowired
    private ICsIpWxCodeService csIpWxCodeService;

    /**
     * 查询ip地址与企业微信匹配列表
     */
    @PreAuthorize("@ss.hasPermi('system:ipWxCode:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsIpWxCode csIpWxCode)
    {
        startPage();
        List<CsIpWxCode> list = csIpWxCodeService.selectCsIpWxCodeList(csIpWxCode);
        return getDataTable(list);
    }

    /**
     * 导出ip地址与企业微信匹配列表
     */
    @PreAuthorize("@ss.hasPermi('system:ipWxCode:export')")
    @Log(title = "ip地址与企业微信匹配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsIpWxCode csIpWxCode)
    {
        List<CsIpWxCode> list = csIpWxCodeService.selectCsIpWxCodeList(csIpWxCode);
        ExcelUtil<CsIpWxCode> util = new ExcelUtil<CsIpWxCode>(CsIpWxCode.class);
        util.exportExcel(response, list, "ip地址与企业微信匹配数据");
    }

    /**
     * 获取ip地址与企业微信匹配详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ipWxCode:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(csIpWxCodeService.selectCsIpWxCodeById(id));
    }

    /**
     * 新增ip地址与企业微信匹配
     */
    @RequestMapping
    public AjaxResult add(HttpServletRequest request)
    {
        CsIpWxCode csIpWxCode = new CsIpWxCode();
        // 获取请求的ip
        String requestIp = IpAddressUtil.getIpAddress(request);
        csIpWxCode.setIpAddress(requestIp);
        return toAjax(csIpWxCodeService.insertCsIpWxCode(csIpWxCode));
    }

    /**
     * 根据ip 获取企业微信图片
     * @return
     */
    @GetMapping("/getWxCodeByIp")
    public AjaxResult getWxCodeByIp(HttpServletRequest request){
        // 获取请求的ip
        String requestIp = IpAddressUtil.getIpAddress(request);
        return success(csIpWxCodeService.selectCsIpWxCodeByIp(requestIp));
    }

    /**
     * 修改ip地址与企业微信匹配
     */
    @PreAuthorize("@ss.hasPermi('system:ipWxCode:edit')")
    @Log(title = "ip地址与企业微信匹配", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsIpWxCode csIpWxCode)
    {
        return toAjax(csIpWxCodeService.updateCsIpWxCode(csIpWxCode));
    }

    /**
     * 删除ip地址与企业微信匹配
     */
    @PreAuthorize("@ss.hasPermi('system:ipWxCode:remove')")
    @Log(title = "ip地址与企业微信匹配", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(csIpWxCodeService.deleteCsIpWxCodeByIds(ids));
    }


    /**
     * 删除全部 信息
     * @return
     */
    @RequestMapping("/removeAll")
    public AjaxResult removeAll()
    {
        return toAjax(csIpWxCodeService.removeAll());
    }

}
